﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using vkUpploaderLib;

namespace vkUpploaderApp
{
    public partial class frmNewAlbum : Form
    {
        private readonly string _groupId;

        public static string NewAlbumName { get; set; }


        public frmNewAlbum(string groupId)
        {
            _groupId = groupId;
            InitializeComponent();
            this.Text = StringResources.NewAlbumForm;

            this.lb_AlbumName.Text = StringResources.NewAlbumName;
            this.lb_PermissionsView.Text = StringResources.PermissionsView;
            this.lb_PermissionsComment.Text = StringResources.PermissionsComment;
            this.btn_Create.Text = StringResources.ButtonCreate;

            bool isGroupAlbum = _groupId == null;
            if (isGroupAlbum)
            {
                this.cbView.DataSource = PermissionsDictionary.PermissionsView;
            }
            else
            {
                this.cbView.DataSource = PermissionsDictionary.PermissionsGroupView;
            }

            this.cbView.DisplayMember = "Value";

            if (isGroupAlbum)
            {
                this.cbComment.DataSource = PermissionsDictionary.PermissionsComment;
            }
            else
            {
                this.cbComment.DataSource = PermissionsDictionary.PermissionsGroupComment;
            }

            this.cbComment.DisplayMember = "Value";
        }


        private void btn_Create_Click(object sender, EventArgs e)
        {
            name_errorProvider.Clear();
            if (tbName.Text == String.Empty)
            {
                name_errorProvider.SetError(tbName, StringResources.NameCannotBeEmpty);
                return;
            }
            if (cbView.SelectedItem is KeyValuePair<int ,string> && cbComment.SelectedItem is KeyValuePair<int ,string>)
            {
                Properties.Settings.Default.DefaultAlbum = ApiHelper.CreateNewAlbum(
                    ((KeyValuePair<int, string>) cbView.SelectedItem).Key,
                    ((KeyValuePair<int, string>) cbComment.SelectedItem).Key,
                    tbName.Text, 
                    _groupId);
                Properties.Settings.Default.DefaultAlbumId = Properties.Settings.Default.DefaultAlbum.Id;
                Properties.Settings.Default.Save();
            }
           
            DialogResult = DialogResult.OK;
        }
    }
}
